package com.sync.utils;

import java.lang.reflect.Method;
import java.nio.MappedByteBuffer;

public class ByteUtils {

	public static byte[] newByte(int capacity) {
		return new byte[capacity];
	}
	// public static byte[] longTobytes(long capacity) {
	//
	// }

	public static boolean compare(byte[] src, int len, byte[] targent) {

		if (len != targent.length) {
			return false;
		}

		for (int i = 0; i < len; i++) {
			if (src[i] != targent[i]) {
				return false;
			}
		}

		return true;
	}

	public static void umap(MappedByteBuffer buffer) {

		try {
			Method cleaner = buffer.getClass().getMethod("cleaner");
			cleaner.setAccessible(true);
			Method clean = Class.forName("sun.misc.Cleaner").getMethod("clean");
			clean.setAccessible(true);
			clean.invoke(cleaner.invoke(buffer));
		} catch (Exception ex) {
		}
		buffer = null;
	}
}
